/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Actors;

import graphics.Images;
import graphics.RenderInstruction;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.awt.image.VolatileImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.EmptyStackException;
import java.util.Stack;
import javax.imageio.ImageIO;
import resources.images.ResourceStub;
import stealth.GameManager;
import stealth.Main;

/**
 *
 * @author Joshua Mabrey
 */
public class PlayerActor extends Actor implements Runnable {

    protected PlayerActor(int up, int down, int left, int right) {
        super();
        upKey = up;
        downKey = down;
        leftKey = left;
        rightKey = right;
        this.x = Main.game.getWidth() / 10;
        this.y = Main.game.getHeight() / 2;
        moves = new Stack<Point>();
        moves.push(new Point((int) x, (int) y));
        shots = new ArrayList<ShotActor>();
        ActorManager.addToCollisionDetect(this);
    }
    private int upKey, downKey, leftKey, rightKey;
    public int orientation = -1;
    public static final int LEFT = 0, RIGHT = 1, UP = 2, DOWN = 3;
    //boolean moveLeft = true, moveRight = true, moveUp = true, moveDown = true;
    public int score = 0;
    public static final int length = 10;

    @Override
    public void act() {
        new Thread(this).start();
    }
    Stack<Point> moves;

    public void revertLocation() {
        try {
            Point p = moves.pop();
            x = p.x;
            y = p.y;
        } catch (EmptyStackException e) {
            this.teleportLocation();
        }
    }

    public void teleportLocation() {
        x = (int) (Math.random() * Main.game.getWidth());
        y = (int) (Math.random() * Main.game.getHeight());
        if (y < 60) {
            y = 65;
            orientation = DOWN;
        }
    }

    @Override
    public RenderInstruction render() {
        return new PlayerActorRender(this);
    }
    double speed = 5.0;
    ArrayList<ShotActor> shots;

    boolean shotOveride = false;

    @Override
    public void run() {
        //vertical = 0;
        /*if (Main.game.keyDown(leftKey)) {
        x -= speed;
        orientation = LEFT;
        }*/
        if (!shotOveride && Main.game.keyDown(rightKey)) {
            //x += speed;
            //orientation = RIGHT;
            shots.add(new ShotActor(this));
            shotOveride = true;
        }
        else if (!Main.game.keyDown(rightKey)){
            shotOveride = false;
        }
        if (Main.game.keyDown(downKey)) {
            y += speed;
            if (y > 675) {
                y -= speed;
            }
            //orientation = DOWN;
        }
        if (Main.game.keyDown(upKey)) {
            y -= speed;
            if (y < 60) {
                y += speed;
            }
            //orientation = UP;
        }
        for (ShotActor a : shots.toArray(new ShotActor[0])) {
            a.act();
            if (a.destroyed) {
                shots.remove(a);
                ActorManager.removeFromCollisionDetection(a);
            }
        }

        ActorManager.handleBounds(this);
        /*if (moves.size() > 9) {
        moves.remove(0);
        moves.remove(0);
        moves.remove(0);
        moves.remove(0);
        moves.remove(0);
        //take earliest 5 moves off of stack if we have at least ten move history
        }*/
        //moves.push(new Point((int) x, (int) y));
        //ActorManager.tempAddBoundingShape(this.collidableArea()[0], false, (short) 1);
        //ActorManager.tempAddBoundingShape(this.collidableArea()[1], false, (short) 1);

    }

    @Override
    public void collide(Actor collidedWith) {
        if (collidedWith == null) {
        }
        if (collidedWith instanceof PlayerActor) {
            /*
             * TODO: implement teleport for both players. It cannot teleport
             * them to //immediate// death. 
             */
        } else if (collidedWith instanceof BlockadeActor) {
        } else if (collidedWith instanceof EnemyActor) {
            GameManager.handleGameEnd();
        } else if (collidedWith instanceof ShotActor) {
        }

    }

    @Override
    public Shape[] collidableArea() {
        Shape[] returnArray = new Shape[2];
        returnArray[0] = new Rectangle((int) x, (int) y, PlayerActor.length, PlayerActor.length);
        //returnArray[1] = new Rectangle((int) x, (int) y, (int) (PlayerActor.length * .75), 20);
        return returnArray;
    }
}

class PlayerActorRender extends RenderInstruction {

    PlayerActor actor;
    private static VolatileImage up_vol, down_vol, left_vol, right_vol;
    private int orig_width, orig_height;

    public PlayerActorRender(PlayerActor a) {
        actor = a;
        try {

            Graphics2D g;
            BufferedImage i = ImageIO.read(ResourceStub.class.getResource("playerSprite.png"));
            orig_width = i.getWidth(null);
            orig_height = i.getHeight(null);

            /*BufferedImage up = i.getSubimage(0, 0, 96, 96);
            BufferedImage left = i.getSubimage(96, 0, 96, 96);
            BufferedImage down = i.getSubimage(192, 0, 96, 96);
             */
            BufferedImage right = i.getSubimage(96, 0, 96, 96);

            /*if (up_vol == null || up_vol.contentsLost()) {
            up_vol = Images.createVolatileImage(96, 96, Transparency.BITMASK);
            g = (Graphics2D) up_vol.getGraphics();
            g.setComposite(AlphaComposite.Src);
            g.setColor(new Color(0, 0, 0, 0));
            g.fillRect(0, 0, up_vol.getWidth(), up_vol.getHeight());
            g.drawImage(up, 0, 0, null);
            g.dispose();
            }
            if (down_vol == null || down_vol.contentsLost()) {
            down_vol = Images.createVolatileImage(96, 96, Transparency.BITMASK);
            g = (Graphics2D) down_vol.getGraphics();
            g.setComposite(AlphaComposite.Src);
            g.setColor(new Color(0, 0, 0, 0));
            g.fillRect(0, 0, down_vol.getWidth(), down_vol.getHeight());
            g.drawImage(down, 0, 0, null);
            g.dispose();
            }
            if (left_vol == null || left_vol.contentsLost()) {
            left_vol = Images.createVolatileImage(96, 96, Transparency.BITMASK);
            g = (Graphics2D) left_vol.getGraphics();
            g.setComposite(AlphaComposite.Src);
            g.setColor(new Color(0, 0, 0, 0));
            g.fillRect(0, 0, left_vol.getWidth(), left_vol.getHeight());
            g.drawImage(left, 0, 0, null);
            g.dispose();
            }*/
            if (right_vol == null || right_vol.contentsLost()) {
                right_vol = Images.createVolatileImage(96, 96, Transparency.BITMASK);
                g = (Graphics2D) right_vol.getGraphics();
                g.setComposite(AlphaComposite.Src);
                g.setColor(new Color(0, 0, 0, 0));
                g.fillRect(0, 0, right_vol.getWidth(), right_vol.getHeight());
                g.drawImage(right, 0, 0, null);
                g.dispose();
            }

        } catch (IOException ex) {
            System.err.println("Could not load file!");
            System.exit(1);
        }
    }

    @Override
    public void render(Graphics2D g2d) {
        /*switch (actor.orientation) {
        case PlayerActor.LEFT:
        g2d.drawImage(right_vol, (int) actor.x, (int) actor.y, null);
        break;
        case PlayerActor.RIGHT:
        g2d.drawImage(left_vol, (int) actor.x, (int) actor.y, null);
        break;
        case PlayerActor.DOWN:
        g2d.drawImage(down_vol, (int) actor.x, (int) actor.y, null);
        break;
        case PlayerActor.UP:
        g2d.drawImage(up_vol, (int) actor.x, (int) actor.y, null);
        break;
        }*/
        g2d.setColor(Color.red);
        for (ShotActor a : actor.shots) {
            if (a != null && !a.destroyed) {
                a.render().render(g2d);
            }
        }
        g2d.drawImage(right_vol, (int) actor.x, (int) actor.y, null);

        //g2d.setColor(Color.RED);
        //g2d.draw(actor.collidableArea()[0]);
        g2d.setFont(Font.getFont(Font.MONOSPACED));
        g2d.drawString("" + actor.score,(int) actor.x+5,(int) actor.y+5);
        //g2d.setColor(Color.WHITE);
    }
}
